//
//  AppDelegate.m
//  17DianYing
//
//  Created by apple on 15/9/1.
//  Copyright (c) 2015年 apple. All rights reserved.
//

#import "AppDelegate.h"
#import "UserLondManager.h"
#import "Reachability.h"
#import "EaseMob.h"
#import "MBProgressHUD.h"


//友盟引入
#import "UMSocial.h"
#import "UMSocialWechatHandler.h"
#import "UMSocialQQHandler.h"
#import "UMSocialSinaSSOHandler.h"

@interface AppDelegate ()<IChatManagerDelegate,MBProgressHUDDelegate>

{
    MBProgressHUD *HUD;
    long long expectedLength;
    long long currentLength;
}

@property(nonatomic,strong)UserLondManager *londManager;
@property (nonatomic,strong)Reachability * reachability;

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [application setStatusBarHidden:NO];
    [application setStatusBarStyle:UIStatusBarStyleLightContent];
    
    //启动页停留时间
    [NSThread sleepForTimeInterval:3];
    
    //友盟
    [self youMeng];

    [[EaseMob sharedInstance] registerSDKWithAppKey:@"zhixinchuanmei#zhixinchuanmei" apnsCertName:@"aps_development"];

    
    
    [[EaseMob sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions];


    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"everLaunched"])
    {
        
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"everLaunched"];
        
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstLaunch"];
        
    }
    
    else
    {
        
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"firstLaunch"];
        
    }
    
    

    //检测用户有没有登陆，如果登陆直接进入主界面
    self.londManager = [UserLondManager share];
     NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *usertoken = [user objectForKey:@"usertoken"];
    NSString *userid = [user objectForKey:@"userid"];
    
    NSUserDefaults *md5 = [NSUserDefaults standardUserDefaults];
    NSString *md5password = [md5 objectForKey:@"md5PassW"];

    
    UIStoryboard *s = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    
    UIViewController *vc = [s instantiateViewControllerWithIdentifier:@"Homepage"];
    
    UIViewController *beginVc = [s instantiateViewControllerWithIdentifier:@"Enteruser"];
    if (usertoken == nil)
    {
        
    }
    else
    {
        HUD = [[MBProgressHUD alloc] initWithView:beginVc.view];
        [beginVc.view addSubview:HUD];
        
        HUD.dimBackground = YES;
        

        HUD.delegate = self;
        
        
        [HUD showWhileExecuting:@selector(myTask) onTarget:self withObject:nil animated:YES];
        
    [self.londManager longwithUserToken:usertoken blockHandle:^(NSDictionary *dic)
    {

        
        
        NSString *str = [NSString stringWithFormat:@"%@",dic[@"status"]];
        
        
        
        
        if ([str isEqualToString:@"0"])
        {
            [[EaseMob sharedInstance].chatManager asyncLoginWithUsername:userid password:md5password completion:^(NSDictionary *loginInfo, EMError *error) {
                if (!error)
                {

                    
                    self.window.rootViewController = vc;
                    
                    [HUD hide:YES];

                }
                else
                {
                    
                }
            } onQueue:nil];
        }
        else
        {

        }
        

    }];
    
    }
    
//    网络监控
    self.reachability=[Reachability reachabilityWithHostName:@"www.baidu.com"];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkStatusChanged:) name:kReachabilityChangedNotification object:nil];
    //启动监视器
    [self.reachability startNotifier];
    
    
    
    return YES;
}

-(void)networkStatusChanged:(NSNotification *)notification
{
    NetworkStatus  status=[self.reachability currentReachabilityStatus];
    switch (status) {
        case NotReachable:
        {
            
            UIAlertView *a = [[UIAlertView alloc]initWithTitle:@"提示" message:@"无网络" delegate:self cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
            [a show];
        }
            break;
        case ReachableViaWiFi:
        {
            UIAlertView *a = [[UIAlertView alloc]initWithTitle:@"提示" message:@"WiFi网络" delegate:self cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
            [a show];
        }
            break;
        case ReachableViaWWAN:
        {
            UIAlertView *a = [[UIAlertView alloc]initWithTitle:@"提示" message:@"移动网络" delegate:self cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
            [a show];
        }
            break;
        default:
            break;
    }
}

-(void)youMeng{
    
    [UMSocialData setAppKey:@"55ff9fd7e0f55a94ec002748"];
    //    [UMSocialData setAppKey:@"507fcab25270157b37000010"];
    //打开调试日志
    [UMSocialData openLog:YES];
    //    openqq123
    //设置微信AppId、appSecret，分享url
    [UMSocialWechatHandler setWXAppId:@"wx3a5c8fc4e2d6fe40" appSecret:@"0f5eaf07ab9b4eb9fe9084bf12f0627d" url:@"http://www.umeng.com/social"];
    
    //    //设置微信AppId，设置分享url，默认使用友盟的网址
//    [UMSocialWechatHandler setWXAppId:@"wx3a5c8fc4e2d6fe40" appSecret:@"0f5eaf07ab9b4eb9fe9084bf12f0627d" url:@"http://www.umeng.com/social"];
    //
    //    //设置分享到QQ空间的应用Id，和分享url 链接
    [UMSocialQQHandler setQQWithAppId:@"1104807217" appKey:@"iEt4q2m77fdpbmT2" url:@"http://www.umeng.com/social"];
    
    //    //打开新浪微博的SSO开关
    [UMSocialSinaSSOHandler openNewSinaSSOWithRedirectURL:@"http://sns.whalecloud.com/sina2/callback"];
}



- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    
    [[EaseMob sharedInstance] applicationDidEnterBackground:application];
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    [[EaseMob sharedInstance] applicationWillEnterForeground:application];
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    [[EaseMob sharedInstance] applicationWillTerminate:application];

}


- (void)myTask {
    // Do something usefull in here instead of sleeping ...
    sleep(6);

}

@end
